package com.fas.core.plugin.shiro.filter;

import java.util.Date;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import nl.bitwalker.useragentutils.UserAgent;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import com.fas.core.plugin.shiro.kit.FasShiroKit;
import com.fas.core.util.IPUtil;
import com.fas.core.util.sequence.Sequence;
import com.fas.hehe.system.model.LoginLog;
import com.fas.hehe.system.model.LoginUser;

public class FasFormAuthenticationFilter extends FormAuthenticationFilter {

	@Override
	public void setLoginUrl(String loginUrl) {
		// TODO Auto-generated method stub
		super.setLoginUrl(loginUrl);
	}

	@Override
	public String getUsernameParam() {
		// TODO Auto-generated method stub
		return super.getUsernameParam();
	}

	@Override
	public void setUsernameParam(String usernameParam) {
		// TODO Auto-generated method stub
		super.setUsernameParam(usernameParam);
	}

	@Override
	public String getPasswordParam() {
		// TODO Auto-generated method stub
		return super.getPasswordParam();
	}

	@Override
	public void setPasswordParam(String passwordParam) {
		// TODO Auto-generated method stub
		super.setPasswordParam(passwordParam);
	}

	@Override
	public String getRememberMeParam() {
		// TODO Auto-generated method stub
		return super.getRememberMeParam();
	}

	@Override
	public void setRememberMeParam(String rememberMeParam) {
		// TODO Auto-generated method stub
		super.setRememberMeParam(rememberMeParam);
	}

	@Override
	public String getFailureKeyAttribute() {
		// TODO Auto-generated method stub
		return super.getFailureKeyAttribute();
	}

	@Override
	public void setFailureKeyAttribute(String failureKeyAttribute) {
		// TODO Auto-generated method stub
		super.setFailureKeyAttribute(failureKeyAttribute);
	}

	@Override
	protected boolean onAccessDenied(ServletRequest request,
			ServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		return super.onAccessDenied(request, response);
	}

	@Override
	protected boolean isLoginSubmission(ServletRequest request,
			ServletResponse response) {
		// TODO Auto-generated method stub
		return super.isLoginSubmission(request, response);
	}

	@Override
	protected AuthenticationToken createToken(ServletRequest request,
			ServletResponse response) {
		// TODO Auto-generated method stub
		return super.createToken(request, response);
	}

	@Override
	protected boolean isRememberMe(ServletRequest request) {
		// TODO Auto-generated method stub
		return super.isRememberMe(request);
	}

	@Override
	protected boolean onLoginSuccess(AuthenticationToken token,
			Subject subject, ServletRequest request, ServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		saveLoginLog((HttpServletRequest)request);
		return super.onLoginSuccess(token, subject, request, response);
	}

	@Override
	protected boolean onLoginFailure(AuthenticationToken token,
			AuthenticationException e, ServletRequest request,
			ServletResponse response) {
		// TODO Auto-generated method stub
		return super.onLoginFailure(token, e, request, response);
	}

	@Override
	protected void setFailureAttribute(ServletRequest request,
			AuthenticationException ae) {
		// TODO Auto-generated method stub
		super.setFailureAttribute(request, ae);
	}

	@Override
	protected String getUsername(ServletRequest request) {
		// TODO Auto-generated method stub
		return super.getUsername(request);
	}

	@Override
	protected String getPassword(ServletRequest request) {
		// TODO Auto-generated method stub
		return super.getPassword(request);
	}


	public void saveLoginLog(HttpServletRequest request){
		LoginUser loginUser = FasShiroKit.loginUser();

        LoginLog loginLog = new LoginLog();
        loginLog.setAccount(FasShiroKit.principal());
        loginLog.setLogId(Sequence.getSequence());
        loginLog.setCompanyId(loginUser.getCompanyId());
        loginLog.setName(loginUser.getUserNameZh());
        loginLog.setLoginTime(new Date());
        loginLog.setIp(IPUtil.getIpAddr(request));
        
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));  
        loginLog.setBrowser(userAgent.getBrowser().getName());
        loginLog.setVersion(userAgent.getBrowserVersion().getMajorVersion());
        loginLog.setOs(userAgent.getOperatingSystem().getName());
        loginLog.setDeviceType(userAgent.getOperatingSystem().getDeviceType().toString());
        
        loginLog.save();
        //TODO  无法记录登录日志 & session超时有问题
	}
}
